package com.wwu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwu.common.utils.AssertUtil;
import com.wwu.common.web.ResultPage;
import com.wwu.system.entity.OperLog;
import com.wwu.system.mapper.OperLogMapper;
import com.wwu.system.service.IOperLogService;
import com.wwu.system.vo.QueryVO;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * 系统操作日志服务实现类
 * @author 一蓑烟雨
 * @date 2021-10-26
 * @version 1.0.0
 */
@Service
public class OperLogServiceImpl extends ServiceImpl<OperLogMapper, OperLog> implements IOperLogService {

    /**
     * 分页查询系统操作日志信息
     * @param pageNum  页码
     * @param pageSize  每页多少条
     * @param operLog   系统操作日志实体
     * @param queryVO   前端查询条件封装实体类
     * @return com.wwu.common.web.ResultPage
     * @author 一蓑烟雨
     * @date 2023/7/27 15:24
     */
    @Override
    public ResultPage getOperLoglistPage(Integer pageNum, Integer pageSize, OperLog operLog, QueryVO queryVO) {
        Page<OperLog> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<OperLog> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(operLog.getUserName())){
            wrapper.like(OperLog::getUserName,operLog.getUserName());
        }
        if(StringUtils.hasText(operLog.getSessionId())){
            wrapper.like(OperLog::getSessionId,operLog.getSessionId());
        }
        if(StringUtils.hasText(operLog.getOperModule())){
            wrapper.like(OperLog::getOperModule,operLog.getOperModule());
        }
        if(null != operLog.getOperStatus()){
            wrapper.eq(OperLog::getOperStatus,operLog.getOperStatus());
        }
        if(StringUtils.hasText(queryVO.getStartDate()) && StringUtils.hasText(queryVO.getEndDate())){
            wrapper.between(OperLog::getOperStartTime,queryVO.getStartDate(),queryVO.getEndDate());
        }
        wrapper.orderByDesc(OperLog::getRecordId);
        IPage<OperLog> operLogPage = this.page(page, wrapper);
        return new ResultPage<>(operLogPage);
    }

    /**
     * 异步保存操作日志
     * @param operLog 系统操作日志实体
     * @author 一蓑烟雨
     * @date 2023/7/27 15:24
     */
    @Async
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveOperLog(OperLog operLog) {
        AssertUtil.isTrue(!StringUtils.hasText(operLog.getSessionId()),"会话id不能为空");
        AssertUtil.isTrue(!(this.save(operLog)),"操作日志信息新增失败！");
    }

    /**
     * 删除操作日志信息
     * @param recordIds 记录id
     * @author 一蓑烟雨
     * @date 2023/7/27 19:39
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteOperLog(List<String> recordIds) {
        if(!ObjectUtils.isEmpty(recordIds)){
            AssertUtil.isTrue(!this.removeByIds(recordIds),"操作日志删除失败！");
        }else{
            AssertUtil.isTrue(!this.remove(new LambdaQueryWrapper<>()),"操作日志清空失败！");
        }
    }

    /**
     * 定时任务删除操作日志
     * @param operLog  操作日志实体
     * @param queryVO 查询条件
     * @author 一蓑烟雨
     * @date 2023/8/16 15:26
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int deleteOperLog(OperLog operLog, QueryVO queryVO) {
        LambdaQueryWrapper<OperLog> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasText(operLog.getSessionId())){
            wrapper.eq(OperLog::getSessionId,operLog.getSessionId());
        }
        if(StringUtils.hasText(queryVO.getStartDate()) && StringUtils.hasText(queryVO.getEndDate())){
            wrapper.between(OperLog::getOperStartTime,queryVO.getStartDate(),queryVO.getEndDate());
        }

        int count  = this.count(wrapper);
        //如果数量大于0则执行删除操作
        if(count > 0){
            if(this.remove(wrapper)){
                return count;
            }else{
                return -1;
            }
        }
        return count;
    }
}
